package com.itheima.controller;


import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.cacheable.Cacheable;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.CheckGroup;
import com.itheima.service.CheckGroupService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.Errors;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * @author W.J.Y
 */
@RestController
@RequestMapping("checkgroup")
public class CheckGroupController {

    @Reference(version = "1.0.0")
    private CheckGroupService checkGroupService;

    /**
     * 新增检查项
     * @param checkGroup 检查组实例
     * @param errors    表单校验失败实例
     * @return  新增操作结果
     */
    @RequestMapping("add")
    /*@PreAuthorize("hasAuthority('CHECKGROUP_ADD')")*/
    public Result add(@RequestBody  CheckGroup checkGroup, Errors errors){
        if(errors.hasErrors()){
            StringBuilder msg = new StringBuilder();
            List<FieldError> fieldErrors = errors.getFieldErrors();
            for (int i = 0; i < fieldErrors.size(); i++) {
                if(i!=fieldErrors.size()-1) {
                    msg.append(fieldErrors.get(i).getDefaultMessage()).append(",");
                }else {
                    msg.append(fieldErrors.get(i).getDefaultMessage()).append("!");
                }
            }
            return new Result(false, msg.toString());
        }
        //调用业务层添加方法
        checkGroupService.add(checkGroup);
        //返回操作结果
        return new Result(true, MessageConstant.ADD_CHECKGROUP_SUCCESS);
    }


    /**
     * 编辑
     * @param checkGroup 检查组实体类
     * @return  编辑操作的结果
     */
    @RequestMapping("update")
    @PreAuthorize("hasAuthority('CHECKGROUP_EDIT')")
    public Result update(@RequestBody CheckGroup checkGroup){
        //调用业务层修改方法
        checkGroupService.update(checkGroup);
        //返回操作结果
        return new Result(true, MessageConstant.EDIT_CHECKGROUP_SUCCESS);
    }

    /**
     * 根据ID查询检查组
     * @param id    检查组ID
     * @return  返回结果
     */
    @GetMapping("findById/{id}")
    @PreAuthorize("hasAuthority('CHECKGROUP_QUERY')")
    public Result findById(@PathVariable int id){
        CheckGroup checkGroup = checkGroupService.findById(id);
        return new Result(true,"查询成功",checkGroup);
    }

    /**
     * 查询所有的检查组
     * @return
     */
    @Cacheable(expire = 600)
    @GetMapping("findAll")
    @PreAuthorize("hasAuthority('CHECKGROUP_QUERY')")
    public Result findById(){
        List<CheckGroup> checkGroups = checkGroupService.findAll();
        System.out.println("这里是查询所有检查组，我从数据库查询了！！！");
        return new Result(true,"查询成功",checkGroups);
    }

    /**
     * 删除
     * @param id 要删除的检查组的ID
     * @return  编辑操作的结果
     */
    @GetMapping("delete/{id}")
    @PreAuthorize("hasAuthority('CHECKGROUP_DELETE')")
    public Result delete(@PathVariable int id){
        //调用业务层删除方法
        checkGroupService.delete(id);
        //返回操作结果
        return new Result(true, MessageConstant.DELETE_CHECKITEM_SUCCESS);
    }

    /**
     * 分页查询方法
     * @param queryPageBean 分页参数实体类
     * @return  分页查询结果，总条数与当前页数据
     */
    @RequestMapping("findPage")
    /*@PreAuthorize("hasAuthority('CHECKGROUP_QUERY')")*/
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){
        PageResult pageResult = checkGroupService.findPage(
                queryPageBean.getCurrentPage(),
                queryPageBean.getPageSize(),
                queryPageBean.getQueryString());
        return pageResult;
    }
}
